package top.ybq87.array;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class MaxSumArray {

    /**
     * 给出二维数组, 父级和子级相加,求最后最大值,要有路径的.
     *
     * @param args
     */
    public static void main(String[] args) {
        Integer[][] arr = {{4}, {4, 8}, {115, 119, 111}, {4, 15, 3, 5}, {7, 1, 10, 5, 15}};
        for (int i = 1; i < arr.length; i++) {
            Integer[] ar1 = arr[i - 1];
            Integer[] ar2 = arr[i];
            Integer[] temp = ar2.clone();

            for (int j = 0; j < ar1.length; j++) {
                int base_val = ar1[j];
                for (int k = j; k <= j + 1; k++) {
                    if (temp[k] + base_val > ar2[k]) {
                        ar2[k] = temp[k] + base_val;
                    }
                }
            }
        }
        Integer integer = Arrays.stream(arr[arr.length - 1]).max(Integer::compare).get();
        System.out.println(">>>>>>>>结果");
        System.out.println(integer);
        test1();
    }


    public static void test1() {
        int result = 0;
        int index = 0;
        int array[][] = {{4}, {4, 8}, {115, 119, 111}, {4, 15, 3, 5}, {7, 1, 10, 5, 15}};
        for (int[] ints : array) {
            Map<String, Integer> sort = sort(ints, index);
            result += sort.get("value");
            index = sort.get("index");
        }
        System.out.println(result);
    }

    public static Map<String, Integer> sort(int array[], int index) {
        HashMap<String, Integer> obj = new HashMap<>();
        if (array.length == 1) {
            obj.put("index", 0);
            obj.put("value", array[0]);
            return obj;
        }
        int a = array[index];
        int b = array[index + 1];
        int max = Math.max(a, b);
        if (a == max) {
            obj.put("index", index);
            obj.put("value", a);
        } else {
            obj.put("index", index + 1);
            obj.put("value", b);
        }

        return obj;
    }
}
